package cn.jx.asc.controller;

import cn.jx.asc.common.constant.netconst;
import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;
import cn.jx.asc.pojo.bo.NoticeBO;
import cn.jx.asc.pojo.entity.Notice;
import cn.jx.asc.parseupload.OkHttpUtil2;
import cn.jx.asc.service.CustomDataService;
import cn.jx.asc.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 通用请求
 *
 * @author lhg
 */
@Api(value = "预警", tags = {"预警相关接口"})
@RestController
@Slf4j
@RequestMapping("/earlyWarning")
public class EarlyWarningController {
    @Autowired
    private CustomDataService customDataService;

    @Autowired
    private NoticeService noticeService;
    @ApiOperation(value = "滑坡预警", notes = "滑坡预警", httpMethod = "GET")
    @GetMapping("/landslideWarning")
    public Result landslideWarning(Integer mountainId){
        Map<String, String> map =new HashMap<>(6);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        final String format = dateFormat.format(date);
         Double landslide =null;
         String s = null;
        if (mountainId==null||mountainId==1){
            landslide=customDataService.calculateLandslide();
            if (landslide.equals(-1.0)){
                return ResultUtils.resultWithoutData(ResultStatus.FAILED,"存在部分阈值异常");
            }
        }else{
            return ResultUtils.resultWithoutData(ResultStatus.FAILED,"除矿山编号为1的其他预警还在开发中");
        }
        if (landslide>=0.5){
            s="滑坡黄色预警";
            map.put(format,s);
        }
        if (landslide>=0.6){
            s="滑坡橙色预警";
            map.put(format,s);
        }
        if (landslide>=0.7){
            s="滑坡红色预警";
            map.put(format,s);
            OkHttpUtil2 okHttpUtil2 = new OkHttpUtil2();
            okHttpUtil2.issue("A,6");
            try {
                Thread.sleep(1000*5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(netconst.CONFIR_WARING==netconst.NO){
            return  ResultUtils.resultWithoutData(ResultStatus.CONFIR_WARING);
            }
        }
        for(String key:map.keySet()){
            Notice notice = new Notice(UUID.randomUUID().toString(),map.get(key),date);
            final boolean save = noticeService.save(notice);
            if (!save){
               log.error("插入失败");
            }
        }
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,map);
    }
    @ApiOperation(value = "通用异常报警", notes = "通用异常报警", httpMethod = "GET")
    @GetMapping("/commonWarning")
    public Result commonWarning(){
        Map<String, String> map =new HashMap<>(6);
        final String s = customDataService.abnormalAirTemperature();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date();
        final String format = dateFormat.format(date);
        if (s!=null){
            map.put(s,format);
        }
        final String s1 = customDataService.abnormalSoilMoisture();
        if (s1!=null){
            map.put(s1,format);
        }
        final String s2 = customDataService.abnormalLiquidLevel();
        if (s2!=null){
            map.put(s2,format);
        }
        final String s3 = customDataService.abnormalSoilTemperature();
        if (s3!=null){
            map.put(s3,format);
        }
        final String s4 = customDataService.abnormalInjectionVolume();
        if (s4!=null){
            map.put(s4,format);
        }
        final String s5 = customDataService.airHumidityIsAbnormal();
        if (s5!=null){
            map.put(s5,format);
        }
        for(String key:map.keySet()){
            Notice notice = new Notice(UUID.randomUUID().toString(),key,date);
            final boolean save = noticeService.save(notice);
            if (!save){
                log.error("插入失败");
            }
        }
       return ResultUtils.resultWithData(ResultStatus.SUCCESS,map);
    }

    @ApiOperation(value = "查询近两天预警信息", notes = "查询近两天预警信息", httpMethod = "GET")
    @GetMapping("/queryNotices")

    public Result queryNotices(){
        final ArrayList<NoticeBO> notices = new ArrayList<>();
        final QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date d1 = new Date();
        final Date d2 = new Date(-1000*60*60*48);
        final String sTime = dateFormat.format(d2);
        final String eTime = dateFormat.format(d1);
        queryWrapper.between("created_time",sTime,eTime);
        queryWrapper.orderByDesc("created_time");
        final List<Notice> list = noticeService.list(queryWrapper);
        for (Notice n :
                list) {
            if (n.getMessage()!=null&&n.getCreatedTime()!=null){
                NoticeBO bo = new NoticeBO();
                bo.setMsg( n.getMessage());
                bo.setDate(dateFormat.format(n.getCreatedTime()));
                notices.add(bo);
            }
        }
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,notices);
    }

}
